package com.penngo.gui.dfdiff.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.penngo.gui.dfdiff.DFMain;
import com.penngo.gui.dfdiff.view.DataNode;
import com.penngo.gui.dfdiff.view.ProjectNode;

import java.io.File;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LimitList extends LinkedList<ProjectNode>{
    private static Logger log = LogUtils.getLogger(LimitList.class);
    private int limit = 20;
    private boolean isLoad = false;
    public void addFirst(ProjectNode e) {
        if(e.getPathLeft() != null && e.getPathLeft().exists()){
            // 检查是否存在，刷新节点
            List<ProjectNode> delNodes = new LinkedList<>();
            for(int i = 0 ; i < this.size() ; i++){
                ProjectNode temp = this.get(i);
                if((e.getPathLeft() != null && temp.getPathLeft() != null && e.getPathLeft().getAbsolutePath().equals(temp.getPathLeft().getAbsolutePath()))
                        && (e.getPathRight() == null || (e.getPathRight() != null && e.getPathRight().getAbsolutePath().equals(temp.getPathRight().getAbsolutePath())) )){
                    delNodes.add(temp);
                }
            }
            this.removeAll(delNodes);

            super.addFirst(e);
            if(size() > limit){
                super.removeLast();
            }
        }

    }

    public boolean offer(ProjectNode e) {
        if(size() > limit){
            super.poll();
        }
        return super.offer(e);
    }

    public void saveHistory(){
        File tempFile = getConfigFile();
        //List<ProjectNode>
        try {
            FileUtil.writeString(JSONUtil.toJsonStr(this), tempFile, "UTF-8");
        }
        catch(Exception e){
            log.log(Level.SEVERE, "保存失败，" + tempFile.getAbsolutePath(), e);
        }
    }

    private File getConfigFile(){
        File dir = ToolUtil.getConfigDir();
        //java.io.tmpdir=C:\Users\admin\AppData\Local\Temp\dfdiff\.dfdiff
        File tempFile = new File(dir, ".history");
        return tempFile;
    }

    public void loadHistory(){
        if(isLoad == false){
            File tempFile = getConfigFile();
            try {
                isLoad = true;
                String content = FileUtil.readString(tempFile, Charset.forName("UTF-8"));
                JSONArray list = JSONUtil.parseArray(content);
                if(list != null && list.size() > 0){
                    for(int index = 0; index < list.size(); index++){
                        JSONObject data = list.getJSONObject(index);
                        String title = data.getStr("title");
                        ProjectNode.ProjectType type = ProjectNode.ProjectType.valueOf(data.getStr("type"));
                        File pathLeft = StrUtil.isEmpty(data.getStr("pathLeft")) == false ? new File(data.getStr("pathLeft")) : null;
                        File pathRight = StrUtil.isEmpty(data.getStr("pathRight")) == false ? new File(data.getStr("pathRight")) : null;
                        if(pathLeft != null){
                            ProjectNode node = new ProjectNode(title, pathLeft, pathRight, type);
                            addLast(node);
                        }
                    }
                }

            }
            catch(IORuntimeException io){
                String message = io.getMessage();
                if(io.getMessage().indexOf("File not exist") > -1){
                    log.info(message);
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }

    }

    public boolean isLoad() {
        return isLoad;
    }

    public void setLoad(boolean load) {
        isLoad = load;
    }
}
